package com.qkl.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qkl.user.dto.InvitationUserDto;
import com.qkl.user.dto.InvitationUserListDto;
import com.qkl.user.entity.InvitationRelation;
import com.qkl.user.entity.User;
import com.qkl.user.entity.UserLevel;
import com.qkl.user.enums.TalentLevelEnum;
import com.qkl.user.enums.UserLevelEnum;
import com.qkl.user.mapper.InvitationRelationMapper;
import com.qkl.user.mapper.UserLevelMapper;
import com.qkl.user.service.InviteRelationService;
import com.qkl.user.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Service
public class InviteRelationServiceImpl implements InviteRelationService {
    @Autowired
    private InvitationRelationMapper invitationRelationMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private UserLevelMapper userLevelMapper;
    @Autowired
    private MatrixServiceImpl matrixService;
    @Override
    public InvitationUserDto directUsers() {
        Long userId = JwtUtils.getUserId();
        return invitationRelationMapper.InvitationUser(userId);
    }

    @Override
    public IPage<InvitationUserListDto> directList(String address, Integer pageNum, Integer pageSize) {
        Long userId = JwtUtils.getUserId();
        QueryWrapper<InvitationRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("inviter_id", userId);
        if(address != null && !address.isEmpty()){
            queryWrapper.eq("u.address", address);
        }
        //queryWrapper.orderByDesc("u.create_time");
        Page<InvitationUserListDto> page = new Page<>(pageNum, pageSize);
        Page<InvitationUserListDto> resultPage = invitationRelationMapper.selectInviteUserPage(page, queryWrapper);
        List<InvitationUserListDto> records = resultPage.getRecords();

        if (!records.isEmpty()) {
            // 1. 提取列表中所有用户的ID
            List<Long> userIds = records.stream()
                    .map(dto -> Long.valueOf(dto.getId())) // 假设 dto.getId() 返回的是用户ID
                    .collect(Collectors.toList());

            // 2. 调用批量方法获取所有用户的等级名称 (仅2次数据库查询，无论列表多长)
//            Map<Long, String> userLevelNameMap = matrixService.batchGetUserTalentLevels(userIds);
//
//            // 3. 在内存中循环设置等级名称 (无数据库查询)
//            for (InvitationUserListDto dto : records) {
//                Long userIdInList = Long.valueOf(dto.getId());
//                String levelName = userLevelNameMap.getOrDefault(userIdInList, "");
//                dto.setLevel(levelName);
//            }
            // 3. 在内存中循环设置等级名称 (无数据库查询)
            for (InvitationUserListDto dto : records) {
                Long id = Long.valueOf(dto.getId());
                String levelName = matrixService.getUserTalentLevel(id);
                //String levelName = userLevelMap.getOrDefault(userId, "");
                dto.setLevel(levelName); // 将拼接好的等级名称设置到DTO中
            }
        }
        // 1. 提取列表中所有被邀请用户的ID
//        List<Long> invitedUserIds = records.stream()
//                .map(dto -> Long.valueOf(dto.getUserId())) // 假设你的DTO有getUserId()方法
//                .collect(Collectors.toList());
//        // 2. 调用批量方法获取所有用户的等级名称 (核心优化点)
//        Map<Long, String> userLevelMap = this.batchGetUserTalentLevels(invitedUserIds);



        return resultPage;
    }



}
